"use strict";
require([
    './dev/layer',
    'polyfill',
    './dev/nav',
    'text!tpl/bookSetup.hbs',
    './conf/baseConfig',
    './dev/helper',
    './dev/http'
], function (layer, polyfill, nav, formSet, baseConfig, helper, http) {
    //layer.loadLayer();
    nav.showNav();
    let interest;

    const formConf = {
        formTit: "文章信息",
        inputTit: "文章标题",
        textType: {
            textName: "文章类型",
            formName: "textType",
            layFilter: "writing",
            selec: [
                {
                    values: "1",
                    types: "党建"
                },
                {
                    values: "2",
                    types: "学习"
                },
                {
                    values: "3",
                    types: "写作"
                },
                {
                    values: "4",
                    types: "体会"
                },
                {
                    values: "5",
                    types: "心得"
                }
            ]
        },
        creater: {
            textName: "创建人",
            formName: "interest",
            layFilter: "creater"
        },
        timeTit: "发布日期"
    };

    const getUserList = function () {
      const dtd = $.Deferred()
      http.commGet(`${baseConfig.serUrl}/a/findUserList`, "", (data) => {
        var ary = [];
        $.each(data, (index, item) => {
         var obj ={
           values:item.id,
           types:item.userName
         };
          ary.push(obj);
        });
        formConf.creater.selec = ary;
        dtd.resolve(formConf);
      }, (err) => {
        console.log(err);
        dtd.reject();
      }, '');
      return dtd.promise();
    };



    getUserList().then((data) => {
      const creater = helper.getSessionHandle("creater");
      interest =helper.findIndexData(creater, data.creater.selec, "types")
      layer.setlayTpl("laytpl", "formBox", formSet, data);
    });

    layer.setlayTpl("laytpl", "formBox", formSet, formConf);

    layui.use(['form', 'layedit', 'laydate'], function () {
        var layer = layui.layer
            , form = layui.form
            , layedit = layui.layedit
            , laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听提交
        form.on('submit(demo1)', function (data) {
            console.log(data)
            const contents = helper.getSessionHandle("contents")
            const imgs = helper.getSessionHandle("imgs")
            const params = {
                createUser: data.field.interest,
                title: data.field.textTit,
                type: data.field.textType,
                url: imgs
                //content: contents
            };

            //console.log(params);

            http.commPost(`${baseConfig.serUrl}/a/addArticle`, params, (data) => {

                if(data){
                    // sessionStorage.removeItem("imgs")
                    // sessionStorage.removeItem("contents")
                    console.log(data);
                    window.open("staff.html", "_self")
                }
            }, (err) => {
                console.log(err);
            }, '');
            return false;
        });




        //表单初始赋值
        form.val('example', {
          "textType": 0
          ,"interest": interest+1
        })



    });




});
